package api

import (
	"aglaia/global"
	"aglaia/internal/service"
	"aglaia/pkg/app"
	"aglaia/pkg/errcode"
	"github.com/gin-gonic/gin"
)

func Categories(c *gin.Context) {
	response := app.NewResponse(c)
	page := app.GetPage(c)
	pageSize := app.GetPageSize(c)

	svc := service.New(c)
	categories, total, err := svc.Categories(page, pageSize)
	if err != nil {
		global.Logger.Infof("svc.Articles err: %v\n", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}

	response.ToResponseList(categories, total)
}

func CreateCategory(c *gin.Context) {
	param := service.CreateCategoryRequest{}
	response := app.NewResponse(c)
	if err := response.Bind(&param); err != nil {
		global.Logger.Infof("response.Bind err: %v\n", err)
		response.ToErrorResponse(errcode.InvalidParams)
		return
	}
	svc := service.New(c)
	if err := svc.CreateCategory(param); err != nil {
		global.Logger.Infof("svc.CreateCategory err: %v\n", err)
		response.ToErrorResponse(errcode.InvalidParams)
		return
	}

	response.ToResponse(nil)
}

func DeleteCategory(c *gin.Context) {
	param := service.DeleteCategoryRequest{}
	response := app.NewResponse(c)
	if err := response.Bind(&param); err != nil {
		global.Logger.Infof("response.Bind err: %v\n", err)
		response.ToErrorResponse(errcode.InvalidParams)
		return
	}

	svc := service.New(c)
	if err := svc.DeleteCategory(param); err != nil {
		global.Logger.Infof("svc.CreateCategory err: %v\n", err)
		response.ToErrorResponse(errcode.InvalidParams)
		return
	}

	response.ToResponse(nil)
}
